/*
 * Level.java
 *
 * Created on 2007. m�rcius 25., 15:50
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package lemmingsproto;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;



/**
 *
 * @author attila
 * Egy p�ly�t reprezent�l� oszt�ly
 */
public class Level {
   
    String name;
    String filename;
    int minPoints;
    ArrayList<Command> mapCommands;
    
    /** Konstruktor */
    public Level() {
        System.out.println("> Level konstriktor lefutott paramter nlkl");
        System.out.println("@ Ez valssznleg hibs fggvnyhvs kell majd legyen (depraced)");
        filename="";
        name="N/A";
        minPoints=0;
    }
    /**
    * �j p�lya konstruktora 
    * @param fn
    * A p�ly�t le�r� file neve
    */
    public Level(String fn) {
        System.out.println("> Level konstriktor lefutott paramterrel");
        System.out.println("@ Paramter: " + fn);
        filename=fn;
        name=fn;
        minPoints=0;
    }
    /**
    * Visszaadja a p�lya teljes�t�s�hez sz�ks�ges pontsz�mot
    * @return 
    * A p�lya teljes�t�s�hez sz�ks�ges pontsz�m.
    */   
    public int getJob() {
        return minPoints;
    }
    
    /**
    * Bet�lti a k�rt p�ly�t
    * @return
    * Egy <code>Stage</code>
    */ 
    public Stage getMap() {
        System.out.println("> Level.getMap()");
        System.out.println("# Itt kell megadni a plyt. A vgs verziban ez majd fjbll fog olvasni. Befejezs: done");
        
        Stage stg=null;
        //BufferedReader map_reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader map_reader=null;
        try{
        map_reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
        }catch(Exception e){
        	e.printStackTrace();
        }
        int i=0;

    	String line;
        boolean hasnext=true;
        
    	while (hasnext){
            Command c;
            try {
        	line=map_reader.readLine();
    		if (line==null) return null;
    		String[] splited=line.split(" ");
    		c=new Command(splited[0]);
    		for (int j=1;j<splited.length;j++){
    			c.setParam(splited[j]);
    		}
            } catch (IOException e) {
                System.out.println("@ IOException kezelve, plyabetlt kilp.");
                return null;
            }
            
            if (c.getName().compareTo("done")==0) {
                hasnext=false;
            }

    	
                if ((c.getName().compareTo("init")==0)){
    			if (c.getParamNum()>=2){
    				stg=new Stage(c.getParamAsInt(0),c.getParamAsInt(1));
    			}
    		}
    		if ((c.getName().compareTo("stone")==0) && (stg!=null)){
    			if (c.getParamNum()>=2){
    				stg.setFieldProp(c.getParamAsInt(0),c.getParamAsInt(1),new Stone(null));
    			}
    		}
    		if ((c.getName().compareTo("start")==0) && (stg!=null)){
    			if (c.getParamNum()>=2){
                            System.out.println("@ start? ");
    				stg.setFieldProp(c.getParamAsInt(0),c.getParamAsInt(1),new Start(null));
    			}
    		}
    		if ((c.getName().compareTo("finish")==0) && (stg!=null)){
    			if (c.getParamNum()>=2){
    				stg.setFieldProp(c.getParamAsInt(0),c.getParamAsInt(1),new Finish(null));
    			}
    		}
    		if ((c.getName().compareTo("border")==0) && (stg!=null)){
    			if (c.getParamNum()>=2){
    				stg.setFieldProp(c.getParamAsInt(0),c.getParamAsInt(1),new Border(null));
    			}
    		}
    		if ((c.getName().compareTo("score")==0) && (stg!=null)){
    			if (c.getParamNum()>0){
    				minPoints=c.getParamAsInt(0);
    			}
    		}
    		if ((c.getName().compareTo("lemmings")==0) && (stg!=null)){
    			if (c.getParamNum()>0){
    				Lemming.setNum(c.getParamAsInt(0));
    			}
    		}
    		if ((c.getName().compareTo("block")==0) && (stg!=null)){
    			GetStoned.setNum(c.getParamAsInt(0));
    		}
    		if ((c.getName().compareTo("explode")==0) && (stg!=null)){
    			Explode.setNum(c.getParamAsInt(0));
    		}
    		if ((c.getName().compareTo("para")==0) && (stg!=null)){
    			Parachute.setNum(c.getParamAsInt(0));
    		}
    		if ((c.getName().compareTo("drill")==0) && (stg!=null)){
    			Drill.setNum(c.getParamAsInt(0));
    		}
    		if ((c.getName().compareTo("build")==0) && (stg!=null)){
    			Build.setNum(c.getParamAsInt(0));
    		}
            
            // kirn a plyamtrixot, tesztelsi cllal, de nem megy:
    		if ((c.getName().compareTo("tostring")==0) && (stg!=null)){
                        
    			if (stg!=null) {
                            System.out.println("# A plya:"+stg.toString());
                            
                        }
    		}
    	}
    	
    	//ez mi a vihar?
        //stg.loadStage(""); 
        
    	return stg;
    }
    /**
    * Visszaadja a p�lya nev�t
    * @return
    * A p�lya neve
    */
    public String getName() {
        return name;
    }
}
